package cn.myweixin.controller;

import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import cn.myweixin.po.Comment;
import cn.myweixin.po.Notice;
import cn.myweixin.po.Trainee;
import cn.myweixin.po.Video;
import cn.myweixin.service.impl.CommentImpl;
import cn.myweixin.service.impl.TraineeImpl;
import cn.myweixin.service.impl.VideoImpl;


@Controller
@RequestMapping("/video")
public class VideoController {
	@Autowired
	 private TraineeImpl traineeImpl;
	@Autowired
	private VideoImpl videoImpl;
	@Autowired
	private CommentImpl   commentImpl;

	@RequestMapping("/queryvideos")
	public  ModelAndView queryvideos() throws Exception{
		
		List<Video> video0=videoImpl.selectByType("作物生产 ");
		List<Video> video1=videoImpl.selectByType("花卉园艺生产  ");
		List<Video> video2=videoImpl.selectByType("动物养殖  ");
		List<Video> video3=videoImpl.selectByType("水产养殖  ");
		List<Video> video4=videoImpl.selectByType("经济林生产  ");
		List<Video> video5=videoImpl.selectByType("农机具使用与维修 ");
		List<Video> video6=videoImpl.selectByType("农产品深加工  ");
		List<Video> video7=videoImpl.selectByType("动物疫病防治  ");
		List<Video> video8=videoImpl.selectByType("理财与融资  ");
		List<Video> video9=videoImpl.selectByType("市场营销与管理  ");
		
		ModelAndView modelAndView=new ModelAndView();
		modelAndView.addObject("video0", video0);
		modelAndView.addObject("video1", video1);
		modelAndView.addObject("video2", video2);
		modelAndView.addObject("video3", video3);
		modelAndView.addObject("video4", video4);
		modelAndView.addObject("video5", video5);
		modelAndView.addObject("video6", video6);
		modelAndView.addObject("video7", video7);
		modelAndView.addObject("video8", video8);
		modelAndView.addObject("video9", video9);
		
		modelAndView.setViewName("video/audoplay");
		
		return modelAndView;
	}
	
	
	@RequestMapping(value="/findvideobyid",method={RequestMethod.POST,RequestMethod.GET})
	public ModelAndView findvideobyid(HttpServletRequest request,HttpServletResponse response,@RequestParam(value="id",required=true) Integer id) throws Exception{
		request.setCharacterEncoding("UTF-8");
		HttpSession session=request.getSession();
		Cookie[] cookies=request.getCookies();
		if(cookies!=null){
		for (Cookie cookie : cookies) {
			if(cookie.getName().equals("vx_openid")){
				    String openid=cookie.getValue();
				    Trainee trainee= traineeImpl.selectByPrimaryKey(openid);
				    ModelAndView modelAndView=new ModelAndView();
				    if(trainee!=null){
				    	session.setAttribute("openid", openid);
				    	Video video=videoImpl.selectByPrimaryKey(id);
						List<Comment> comments=commentImpl.selectByVideo(id);
						modelAndView.addObject("user", trainee);
						modelAndView.addObject("video", video);
						modelAndView.addObject("comments", comments);
						modelAndView.setViewName("video/singleplay");
				    	return modelAndView;
				    }
				    else{
				    	
				    	return new ModelAndView(new RedirectView("user/register.action"));
				    	
				    }
				}
			}
		}
		
		return new ModelAndView(new RedirectView("user/cookievalidate.action"));
		
	}
	
	@RequestMapping(value="/insertcomment",method={RequestMethod.POST,RequestMethod.GET})
	public void insertcomment(HttpServletRequest request,HttpServletResponse response,@RequestParam(value="vid",required=true) Integer vid,@RequestParam(value="area",required=true) String area) throws Exception{
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		HttpSession session=request.getSession();
	
		System.out.println("视频id为："+vid);
		System.out.println("评论为："+area);
		String tid=(String) session.getAttribute("openid");
		
		Comment comment=new Comment();
		comment.setVideoId(vid);
		comment.setTrainId(tid);
		comment.setArea(area);
		PrintWriter out=response.getWriter();
		if(commentImpl.insert(comment)==1){
			out.print("发表成功!");
			
		}
		else
			out.print("发表失败!");
		out.flush();
		out.close();
		
	}

}
